//	AUTHOR:			ENRICO CANTONI
//	CREATED:		NOVEMBER 15, 2017
//	MODIFIED: 		NOVEMBER 7, 2022
//	DESCRIPTION:	Plots turnout effect of kids by children's age in years
//					(Figure 4).


// Set Stata version
version 16.1

********************************************************************************
********************************************************************************

// Samples
local s_1			s_kids
local name_1		Figure_4			// Full children's sample

// Outcome
local Y				voted

// Regressors of interest
local X				anykids_?X* anykids_??X* anykids_18moreX*

// Covariates
#delimit ;
local covs
	year##nbhd
	precinct_age
	precinct_citizen
	precinct_female
	precinct_hh_income_mod
	hh_citizen
	hh_income_mod
	income
	irpef
	female##maritalstatus
;
#delimit cr

// X-axis titles
local xtitle 		"Any children of age..."

// Y-axis labels by graph
local ylbl_1		-.04(.02).02
forvalues x = 2/12 {
	local ylbl_`x'	-.08(.02).02
}

********************************************************************************
********************************************************************************

*** GRAPH FOR DUMMIES OF CHILDREN'S AGE ***

// Loop over samples
forvalues s = 1/1 {

	// Load data for given sample
	use ${analysis_clean} if `s_`s''

	// Initialize matrix of results
	local n_cols = 10
	local n_rows = 19
	mat results = J(`n_rows', `n_cols', 0)
	#delimit ;
	local colnames "age b_f cimin_f cimax_f
						b_m cimin_m cimax_m
						b_d cimin_d cimax_d"
	;
	#delimit cr
	matrix colnames results = `colnames'

	// Run regression
	reghdfe `Y' `covs' `X', absorb(age#female ID_voter) cluster(ID_hh ID_voter)

	// Store estimates
	local row = 1
	local ages ""
	forvalues age = 0/17 {
		local ages "`ages' `age'"
	}
	local ages "`ages' 18more"
	foreach age of local ages {

		// Initialize col counter
		local col = 2

		// Store age in first col
		mat results[`row', 1] = real(regexr("`age'", "([A-Za-z]+)", ""))

		// Store gender-specific impact estimates
		foreach v of varlist anykids_`age'X* {
			mat results[`row', `col'] = _b[`v']
			#delimit ;
			mat results[`row', `col'+1] = _b[`v'] -
				invttail(e(df_r),0.025)*_se[`v']
			;
			mat results[`row', `col'+2] = _b[`v'] +
				invttail(e(df_r),0.025)*_se[`v']
			;
			#delimit cr
			
			// Increase col counter
			local col = `col'+3
		}

		// Store female-male difference
		lincom anykids_`age'Xfemale - anykids_`age'Xmale
		mat results[`row', `col'] = r(estimate)
		mat results[`row', `col'+1] = r(estimate) - invttail(r(df), 0.025)*r(se)
		mat results[`row', `col'+2] = r(estimate) + invttail(r(df), 0.025)*r(se)
		local ++row
	}

	// Transform matrix of results to data
	clear
	svmat results, names(col)

	// Reshape long to separate stats for males, females, and f-m diff
	reshape long b_ cimin_ cimax_, i(age) j(temp) string
	replace age = age - .1 if temp == "f"
	replace age = age + .1 if temp == "m"

	// Make graph for two genders
	local output ${output_graphs}/`name_`s''_a
	#delimit ;
	twoway 	(rcap cimin cimax age if temp == "f", lcolor(orange))
			(rcap cimin cimax age if temp == "m", lcolor(navy))
			(connected b age if temp == "f",
				lcolor(none) lpattern(dash) mcolor(orange) sort(t))
			(connected b age if temp == "m",
				lcolor(none) lpattern(dash) mcolor(navy) sort(t)),
			graphregion(fcolor(white) lcolor(white) margin(zero))
			legend(off)
			xlabel(0 5 10 15 18 "18+")
			xtitle(`xtitle')
			ylabel(`ylbl_`s'', nogrid)
			yline(0, lcolor(gs9) lpattern(shortdash))
			ytitle("Voter Turnout (Coefficient)")
			saving(`output'.gph, replace)
	;
	#delimit cr
	graph export `output'.pdf, replace

	// Make graph for f-m difference
	local output ${output_graphs}/`name_`s''_b
	#delimit ;
	twoway	(rcap cimin cimax age if temp == "d", lcolor(cranberry))
			(connected b age if temp == "d",
				lcolor(cranberry) lpatter(solid) mcolor(cranberry) sort(t)),
			graphregion(fcolor(white) lcolor(white) margin(zero))
			legend(off)
			xlabel(0 5 10 15 18 "18+")
			xtitle(`xtitle')
			ylabel(`ylbl_`s'', nogrid)
			yline(0, lcolor(gs9) lpattern(shortdash))
			ytitle("Female-Male Difference (Coefficient)")
			saving(`output'.gph, replace)
	;
	#delimit cr
	graph export `output'.pdf, replace
	clear
}
